//
//  KuaixunCell.swift
//  YunshuApp
//
//  Created by 周文杰 on 2017/6/2.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

class KuaixunCell: UITableViewCell {

    let dateLabel = UILabel()
    let loveImg = UIImageView()
    let loveCount = UILabel()
    
    let titleLabel = UILabel()
    let dotView = UIView()
    let line = UIView()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.titleLabel.font = UIFont.systemFont(ofSize: 14)
        self.titleLabel.textColor = UIColor.black
        self.titleLabel.numberOfLines = 2
        self.contentView.addSubview(self.titleLabel)
        
        self.dotView.frame = CGRect.init(x: 10, y: 0, width: 5, height: 5)
        self.dotView.backgroundColor = Const.WTM_LIGHTGRAY
        self.dotView.layer.cornerRadius = 2.5
        self.contentView.addSubview(self.dotView)

        self.dateLabel.font = UIFont.systemFont(ofSize: 11)
        self.dateLabel.textColor = Const.COLOR_GRAY
        self.contentView.addSubview(self.dateLabel)
        
        self.loveCount.font = UIFont.systemFont(ofSize: 11)
        self.loveCount.textColor = Const.COLOR_GRAY
        self.contentView.addSubview(self.loveCount)
        
        self.loveImg.image = UIImage.init(named: "favourate1")
        self.contentView.addSubview(self.loveImg)
        
        self.line.backgroundColor = Const.WTM_LIGHTGRAY
        self.contentView.addSubview(self.line)

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setTitle(_ text: String) {
        self.titleLabel.text = text
        let width = (Const.SCREEN_WIDTH - 35 )
        
        var height = UIUtility.calcStringHeight(text, font: self.titleLabel.font, refWidth: width)
        if height > 40 {
            height = 40
        }
        self.titleLabel.frame = CGRect.init(x: 25, y: 10, width: width, height: height)
        self.dateLabel.frame = CGRect.init(x: 25, y: 10 + height + 10, width: 120, height: 13)
        var center = self.dotView.center
        center.y = (height + 33) / 2
        self.dotView.center = center
        
        self.loveImg.frame = CGRect.init(x: Const.SCREEN_WIDTH - 40, y: 10 + height + 10, width: 15, height: 15)
        
        self.loveCount.frame = CGRect.init(x: Const.SCREEN_WIDTH - 20, y: 10 + height + 10, width: 15, height: 15)
        
        self.line.frame = CGRect.init(x: 0, y: height + 45 - Const.ONE_PIXEL, width: Const.SCREEN_WIDTH, height: Const.ONE_PIXEL)
    }
    
    func setUnread(_ isUnread: Bool) -> Void {
        if isUnread {
            self.dotView.backgroundColor = UIColor.red
        }else {
            self.dotView.backgroundColor = Const.WTM_LIGHTGRAY
        }
    }

    static func heightWith(_ text:String) -> CGFloat {
        
        let width = (Const.SCREEN_WIDTH - 35 )

        var height = UIUtility.calcStringHeight(text, font: UIFont.systemFont(ofSize: 14), refWidth: width)
        if height > 40 {
            height = 40
        }
        return 10 + height + 10 + 25
    }
}
